﻿using Prism.Commands;
using Prism.Mvvm;
using Prism.Interactivity.InteractionRequest;
using System;
using System.Collections.Generic;
using System.Linq;
using Prism.Events;
using SPW.Infrastructure.Repositorys;
using Prism.Regions;
using Prism.Modularity;
using CommonServiceLocator;
using SPW.Infrastructure.Interfaces;
using SPW.Infrastructure.Utils;
using System.Windows.Input;

namespace SPW.GenericDesktop.ViewModels
{
    public class SkylineWindowViewModel : BindableBase, IInteractionRequestAware
    {
        private readonly IEventAggregator _eventAggregator;
        private readonly IRegionManager _regionManager;
        private readonly IModuleManager _moduleManager;

        private string _title;
        public string Title
        {
            get => _title;
            set => SetProperty(ref _title, value);
        }

        public SkylineWindowViewModel(IEventAggregator eventAggregator,
            IRegionManager regionManager,
            IModuleManager moduleManager)
        {

            this._eventAggregator = eventAggregator;
            this._regionManager = regionManager;
            this._moduleManager = moduleManager;
            //加载容器
            InitLayoutView();
            //设置标题
            Title = Properties.Settings.Default.SoftwareMasterTitle;
            this.ModuleContainerRequest = new InteractionRequest<INotification>();
        }

        private void InitLayoutView()
        {
            //加载布局模块
            var layoutViewInst = ServiceLocator.Current.GetInstance<ILayoutView>(Properties.Settings.Default.LayoutView);
            this._regionManager.RegisterViewWithRegion("LayoutRegion", layoutViewInst.GetType());
        }

        private void EarthRenderEventHandler(Infrastructure.Events.EarthRenderEvent eventArgs)
        {
            if(eventArgs.IsSuccess)
            {
                ModuleContainerRequest.Raise(new Confirmation { Title = "模块容器窗口" });
            }
        }

        public INotification Notification { get; set; }
        public Action FinishInteraction { get; set; }

        public InteractionRequest<INotification> ModuleContainerRequest { get; set; }

        public ICommand LoadedCommand
        {
            get => new DelegateCommand<AxTerraExplorerX.AxTE3DWindow>((ocx) =>
            {
                TerraExplorerX.ISGWorld701 sGWorld = new TerraExplorerX.SGWorld701();
                //初始化事件
                if (sGWorld is TerraExplorerX._ISGWorld701Events_Event viewerEvents)
                {
                    viewerEvents.OnSGWorldMessage += ViewerEvents_OnSGWorldMessage;//加载消息接管
                    viewerEvents.OnLoadFinished += ViewerEvents_OnLoadFinished;//加载完成事件
                }
                //加载地球
                sGWorld.Project.Open($"{System.AppDomain.CurrentDomain.BaseDirectory}\\App_Data\\Default\\default_local.FLY", true);
            });
        }

        private void ViewerEvents_OnLoadFinished(bool bSuccess)
        {
            // 发送消息
            EventAggregatorRepository.GetInstance().eventAggregator
                .GetEvent<Infrastructure.Events.EarthRenderEvent>()
                .Publish(new Infrastructure.Events.EarthRenderEvent(bSuccess));
            // 根据结果觉得是否加载模块容器
            if(bSuccess) ModuleContainerRequest.Raise(new Confirmation { Title = "模块容器窗口" });
        }

        private bool ViewerEvents_OnSGWorldMessage(string MessageID, string SourceObjectID)
        {
            return false;
        }
    }
}
